package com.fr.data.core.db.tableObject;

import com.fr.base.FRContext;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import com.fr.data.core.db.dialect.OracleDialect;
import com.fr.data.core.db.dml.CreateTable;
import com.fr.data.core.db.dml.Select;
import com.fr.data.core.db.dml.Table;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class AbstractTableObject
  implements TableObject
{
  protected List columnsList = new ArrayList();

  public AbstractTableObject()
  {
    initColumns();
  }

  protected Column getColumn(int paramInt)
  {
    return ((Column)this.columnsList.get(paramInt));
  }

  protected void addColumn(String paramString, int paramInt, ColumnSize paramColumnSize)
  {
    addColumn(paramString, paramInt, paramColumnSize, true);
  }

  protected void addColumn(String paramString, int paramInt, ColumnSize paramColumnSize, boolean paramBoolean)
  {
    addColumn(paramString, paramInt, paramColumnSize, paramBoolean, false);
  }

  protected void addColumn(String paramString, int paramInt, ColumnSize paramColumnSize, boolean paramBoolean1, boolean paramBoolean2)
  {
    addColumn(paramString, paramInt, paramColumnSize, paramBoolean1, paramBoolean2, false, false);
  }

  protected void addColumn(String paramString, int paramInt, ColumnSize paramColumnSize, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, boolean paramBoolean4)
  {
    Column localColumn = new Column(paramString, paramInt, paramColumnSize, null, paramBoolean1, paramBoolean2, paramBoolean3, paramBoolean4);
    this.columnsList.add(localColumn);
  }

  public boolean checkExist(Connection paramConnection)
  {
    1 local1 = new Select(this, getTable(), DialectFactory.generateDialect(paramConnection))
    {
      private final AbstractTableObject this$0;

      public String toStatementSQLString()
      {
        return noResultSQL();
      }
    };
    local1.setEmptyWhereAllowed(true);
    int i = 0;
    int j = this.columnsList.size();
    while (i < j)
    {
      local1.addColumnName(getColumn(i).getName());
      ++i;
    }
    PreparedStatement localPreparedStatement = null;
    try
    {
      localPreparedStatement = local1.createPreparedStatement(paramConnection);
    }
    catch (SQLException localSQLException1)
    {
      return false;
    }
    try
    {
      localPreparedStatement.executeQuery();
      localPreparedStatement.close();
    }
    catch (SQLException localSQLException2)
    {
      try
      {
        localPreparedStatement.close();
      }
      catch (SQLException localSQLException3)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException3.getMessage(), localSQLException3);
      }
      return false;
    }
    return true;
  }

  public void createTable(Connection paramConnection)
  {
    if (checkExist(paramConnection))
      return;
    PreparedStatement localPreparedStatement = null;
    dropTable(paramConnection);
    Dialect localDialect = DialectFactory.generateDialect(paramConnection);
    CreateTable localCreateTable = new CreateTable(getTable(), localDialect);
    int i = 0;
    int j = this.columnsList.size();
    while (i < j)
    {
      localCreateTable.addColumn(getColumn(i));
      ++i;
    }
    localCreateTable.setEmptyWhereAllowed(true);
    localCreateTable.setCheckFilter(checkFilter());
    localCreateTable.setForeignReference(foreignReference());
    try
    {
      localPreparedStatement = localCreateTable.createPreparedStatement(paramConnection);
      localPreparedStatement.execute();
      paramConnection.commit();
      FRContext.getLogger().log(Level.WARNING, "Table " + getTable().getName() + " has been created successfully!");
      localPreparedStatement.close();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      try
      {
        localPreparedStatement.close();
      }
      catch (SQLException localSQLException)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
      }
    }
    if (localDialect instanceof OracleDialect)
      ((OracleDialect)localDialect).createTrigger(paramConnection);
  }

  public void dropTable(Connection paramConnection)
  {
    PreparedStatement localPreparedStatement = null;
    try
    {
      localPreparedStatement = paramConnection.prepareStatement("DROP TABLE " + DialectFactory.generateDialect(paramConnection).column2SQL(getTable().getName()));
      localPreparedStatement.execute();
      FRContext.getLogger().log(Level.WARNING, "Table " + getTable().getName() + " has been deleted successfully");
      localPreparedStatement.close();
      paramConnection.commit();
    }
    catch (SQLException localSQLException1)
    {
      try
      {
        localPreparedStatement.close();
      }
      catch (SQLException localSQLException2)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException2.getMessage(), localSQLException2);
      }
    }
  }

  public String checkFilter()
  {
    return null;
  }
}